# -*- coding: utf-8 -*-

import math
from uiview.main_ui import Ui_MainWindow as MainUI
from model.db import Db
from PyQt5.QtSql import QSqlQueryModel, QSqlQuery
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QHeaderView, QAbstractItemView, QMessageBox
from window.pass_form import PassFormWin

class PassFormModel(QSqlQueryModel):

    def __init__(self, parent=None):
        super(PassFormModel, self).__init__(parent)

    def initTable(self):
        self.setHeaderData(0, Qt.Horizontal, "编号")
        self.setHeaderData(1, Qt.Horizontal, "主机")
        self.setHeaderData(2, Qt.Horizontal, "账号")
        self.setHeaderData(3, Qt.Horizontal, "密码")
        self.setHeaderData(4, Qt.Horizontal, "端口")
        self.setHeaderData(5, Qt.Horizontal, "备注")
        self.setHeaderData(6, Qt.Horizontal, "创建时间")

    def data(self, index, role=None):
        if role == Qt.TextAlignmentRole:
            return Qt.AlignCenter
        else:
            return super().data(index, role)

class MainWin(MainUI, QMainWindow):

    currentPage = 0

    pageRecordCount = 10

    totalPage = 0

    def __init__(self):
        super(MainWin, self).__init__()
        self.setupUi(self)
        Db.instance()
        self.model = PassFormModel(self)
        self.recordQuery(0)
        self.model.initTable();
        self.tableView.setModel(self.model)

        self.tableView.setColumnHidden(0, True)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.tableView.resizeColumnsToContents()
        self.tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
        #self.tableView.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
        #self.tableView.horizontalHeader().setSectionResizeMode(2, QHeaderView.Fixed)
        #self.tableView.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
        #self.tableView.horizontalHeader().setSectionResizeMode(4, QHeaderView.Stretch)
        #self.tableView.horizontalHeader().setSectionResizeMode(5, QHeaderView.Stretch)
        #self.tableView.horizontalHeader().setSectionResizeMode(6, QHeaderView.ResizeToContents)

        self.tableView.setColumnWidth(0, 80)
        self.tableView.setColumnWidth(1, 150)
        self.tableView.setColumnWidth(2, 150)
        self.tableView.setColumnWidth(3, 150)
        self.tableView.setColumnWidth(4, 150)
        self.tableView.setColumnWidth(5, 150)
        self.tableView.setColumnWidth(6, 150)

        self.actionaddpass.triggered.connect(self.addRow)
        self.actionrmpass.triggered.connect(self.rmRow)
        self.prevPage.clicked.connect(self.prevPageBtn)
        self.nextPage.clicked.connect(self.nextPageBtn)
        self.switchPage.clicked.connect(self.switchPageBtn)

    def updateStatus(self):
        pass

    def switchPageBtn(self):
        pass

    def prevPageBtn(self):
        if (self.currentPage >= 1):
            page = (self.currentPage - 1) * self.pageRecordCount
            print(page)
            self.recordQuery(page)
            self.currentPage -= 1
            self.updateStatus()
        else:
            QMessageBox.about(self, "系统提示", "已到达第一页")

    def nextPageBtn(self):
        self.currentPage += 1
        if (self.currentPage < self.totalPage):
            page = self.currentPage * self.pageRecordCount
            self.recordQuery(page)
            self.updateStatus()
        else:
            self.currentPage -= 1
            QMessageBox.about(self, "系统提示", "已到达最后一页")

    def recordQuery(self, page):
        query = QSqlQuery()
        query.exec("SELECT COUNT(*) FROM password")
        query.next()
        data = query.value(0);
        self.totalPage = math.ceil(data / self.pageRecordCount)
        szQuery = ("SELECT * FROM password ORDER BY id DESC LIMIT %d, %d" % (page, self.pageRecordCount))
        self.model.setQuery(szQuery)

    def addRow(self):
        pass_form = PassFormWin()
        pass_form.main_handle = self
        pass_form.exec_()

    def rmRow(self):
        reply = QMessageBox.information(self, "系统提示", "确定删除该条数据吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if (reply == QMessageBox.Yes):
            query = QSqlQuery()
            #print(self.tableView.currentIndex().row())
            rows = self.tableView.selectedIndexes()
            for row in reversed(sorted(rows)):
                if row.column() == 1:
                    item = self.model.index(row.row(), 0)
                    query.exec("DELETE FROM password WHERE id = %d" % item.data())

            self.recordQuery(self.currentPage)
            QMessageBox.about(self, "系统提示", "删除成功")
